﻿using System;
using System.Net;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace BallMadness
{
    public class Enemy
    {
        public Vector2 position;
        TimeSpan timeToHit;
        TimeSpan totalTimeToHit = new TimeSpan(0, 0, 3);
        TimeSpan timeToDisappear;
        TimeSpan totalTimeToDisappear = new TimeSpan(0, 0, 0, 3, 500);
        TimeSpan showTime = new TimeSpan(0, 0, 0, 0, 250);

        private float angle;

        public Enemy()
        {
            position.X = Worm.randPosition.Next(0, SharedGraphicsDeviceManager.Current.GraphicsDevice.Viewport.Width);
            position.Y = Worm.randPosition.Next(0, SharedGraphicsDeviceManager.Current.GraphicsDevice.Viewport.Height);
            timeToHit = totalTimeToHit;
            timeToDisappear = totalTimeToDisappear;

            angle = Worm.randPosition.Next(0, 100) / 100f * (float)Math.PI * 2;
        }

        public void OnDraw(SpriteBatch spriteBatch)
        {
            Texture2D use = EnemyManager.shadow;
            if (isHit()) use = EnemyManager.paw;

            float scale = 1.0f;
            if (timeToHit < showTime && timeToHit >= TimeSpan.Zero)
            {
                use = EnemyManager.paw;
                scale = 1.0f + (float)(timeToHit.TotalSeconds / showTime.TotalSeconds) * 10;
            }

            spriteBatch.Draw(
                use,
                position,
                null,
                Color.White,
                angle,
                new Vector2(EnemyManager.shadow.Width / 2, EnemyManager.shadow.Height / 2),
                scale,
                SpriteEffects.None,
                1.0f);
        }

        private bool hit = false;

        public void OnUpdate(GameTimerEventArgs e)
        {
            timeToDisappear -= e.ElapsedTime;
            timeToHit -= e.ElapsedTime;

            if (!hit && isHit())
            {
                hit = true;
                ScoreManager.slam.Play();
                Microsoft.Devices.VibrateController.Default.Start(TimeSpan.FromMilliseconds(10));
            }
        }

        public bool isExpired()
        {
            return timeToDisappear <= TimeSpan.Zero;
        }

        public bool isHit()
        {
            return timeToHit <= TimeSpan.Zero;
        }
    }
}
